/**
 * Created by qiang on 2017-03-20 .
 */
// sessionStorage.token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJvcGVuaWQiOiJvLV92dDBhRVBJUzBHU1kzandSeHBXY2w0ay1VIiwiYWNjZXNzX3Rva2VuIjoiNl80RDl3VmdzZjlSUktUSmpfcEF6eVB1NUJRMXJWS3MzR1dRelVWRUpMbkNBX1ZfeVBKanpjUlFjOWx2dHBlWWdmUlcxWDdUeXl0MW1SdVIwTkZ3dXJHZyIsInJlZnJlc2hfdG9rZW4iOiI2X3NKY1JFT3ZRVVZicEl6TFlxemkyRUVkYThpWXVYaHNMdjFkMTNtcXNGNGNkbXE0Z0k5QkN3eEVxMFlLT3cyX0JvNE5zMkxRMHZodWxWMVpoNElQTVpRIiwiZXhwaXJlc19pbiI6MTUxNzQ3MTA1NzI5OCwiaW52YWxpZF9pbiI6MTUxNzcyMzA1NzI5OCwiaWF0IjoxNTE3NDYzODU3fQ.KQileIKSKnDajANKE7fcUiEItoIXqxG2iOeROIF3YTo'
// sessionStorage.userInfo='{"openid":"o-_vt0aEPIS0GSY3jwRxpWcl4k-U","nickname":"咩呀。","sex":1,"language":"zh_CN","city":"厦门","province":"福建","country":"中国","headimgurl":"http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLv9HJIW4OACo6TRPt4RzUXW3ibfMkoNzugfgANjLYyY1HKvL00ScusibYbVOtvmu9QZv4dh5ricD8Ww/132","privilege":[],"unionid":"opxeQ0qBW81vnR4481DgIhqJ59z0"}'

var self;
var vm = new Vue({
    el: "#app",
    data: {
        userInfo: null,
        chatList: [],
        selectValue: '',
        scrollTop: 0
    },
    mounted: function () {
        self = this;
        common.auth(function () {
            if (sessionStorage.getItem("IsNewUser") != 'T') {
                self.getUserDetail(self.getChatList);
            } else {
                weui.alert("请先完成注册", function () {
                    location.href = 'userInfo.html';
                })
            }
        });
        common.YMCount();
    },
    methods: {
        scrollList: function (event) {
            // console.log(event.target.scrollTop);
            self.MFScrollTop = event.target.scrollTop;
        },
        formatNum: function (num) {
            return parseInt(num)
        },
        createChat: function (item) {
            sessionStorage.setItem("MFScrollTop", self.MFScrollTop);
            location.href = 'message.html?openid=' + item.openid + '&chatNo=' + (item.UserNo > self.userInfo.UserNo ? self.userInfo.UserNo + '' + item.UserNo : item.UserNo + '' + self.userInfo.UserNo) + '&userName=' + item.NickName + '&IsBlack=' + item.IsBlack + '&BeBlack=' + item.BeBlack
            // location.href = 'message.html?openid=' + item.openid + '&chatNo=' + (item.UserNo > self.userInfo.UserNo ? self.userInfo.UserNo + '' + item.UserNo : item.UserNo + '' + self.userInfo.UserNo) + '&userName=' + item.NickName
        },
        getUserDetail: function (callback) {
            service.getUserDetail({}, function (data) {
              //  console.log(data)
                if (data && data.userInfo) {
                    self.userInfo = data.userInfo;
                }
                if (callback) {
                    callback()
                }
                setInterval(self.getChatList, 3000)
            })
        },
        getChatList: function () {
            var getChatListCallBack = function (res) {
              //  console.log(res);
                self.chatList = res.data;
                for (var i = 0; i < res.data.length; i++) {
                    if (res.data[i].Province && res.data[i].City) {
                        res.data[i].cityAttr = common.getCity(res.data[i].Province)[parseInt(res.data[i].City)];
                    }
                    if (res.data[i].Birthday) {
                        res.data[i].age = new Date().getFullYear() - parseInt(res.data[i].Birthday.substr(0, 4)) + 1;
                        res.data[i].constellation = common.getAstro(new Date(res.data[i].Birthday).getMonth() + 1, new Date(res.data[i].Birthday).getDate());
                    }
                }
                self.$nextTick(function () {
                    common.lazyLoadImg();
                    var scrollTop = sessionStorage.getItem("MFScrollTop");
                    scrollTop && (document.querySelector(".user-list").scrollTop = scrollTop);
                });
                $(".loading-bg").hide();
            };
            var options = {
                ChatListParams: self.selectValue
            }
            service.getChatList(options, getChatListCallBack)
        },
        formatDate: function (date) {
            if (new Date(date).toDateString() === new Date().toDateString()) {
                return new Date(date).Format("hh:mm")
            } else if (new Date(date).Format("yyyy-MM") == new Date().Format("yyyy-MM")) {
                if (new Date().getDate() - new Date(date).getDate() == 1) {
                    return "昨天"
                } else if (new Date().getDate() - new Date(date).getDate() == 2) {
                    return "前天"
                } else {
                    return new Date(date).Format("MM-dd")
                }
            } else if (new Date(date) < new Date()) {
                return new Date(date).Format("MM-dd")
            }
        },
        jumpToUserDetail: function (item) {
          //  console.log(item);
            if (item.IsBlack) {
                weui.alert("ta已拉黑你，无法查看");
            } else {
                sessionStorage.setItem("MFScrollTop", self.MFScrollTop);
                location.href = 'userInfo.html?toOpenId=' + item.openid
            }
        }
    }
})